#include"BackGroundLayer2.h"
#include"Common.h"
#include"UserRecord.h"
USING_NS_CC;

bool BackGroundLayer2::init()
{
	if (Layer::init())
	{
		this->setTouchEnabled(true);
		auto touchListener = EventListenerTouchAllAtOnce::create();
		touchListener->onTouchesEnded = CC_CALLBACK_2(BackGroundLayer2::onTouchesEnded, this);
		_eventDispatcher->addEventListenerWithSceneGraphPriority(touchListener, this);
		
		initBox2dWorld();

		auto backSize = Director::getInstance()->getWinSize();
		auto bgSprine = CCSprite::create("bg2.png");
		bgSprine->setPosition(backSize.width*0.5, backSize.height*0.5);
		this->addChild(bgSprine);
		
		CCNode* pNode = CCNode::create();
		this->addChild(pNode);

		CCMenuItemImage* pCloseItem = CCMenuItemImage::create("closeNormal.png",
			"closeSelected.png",
			this,
			menu_selector(BackGroundLayer2::CallCloseBack));
		pCloseItem->setScale(0.5f);
		CCMenu* pMenu = CCMenu::create(pCloseItem,nullptr);
		pMenu->setPosition(backSize.width - 50, 20);
		pNode->addChild(pMenu);

		this->scheduleUpdate();
		return true;
	}
	return false;
}

void BackGroundLayer2::initBox2dWorld()
{
	auto size = CCDirector::sharedDirector()->getWinSize();
	b2Vec2 gravity;
	gravity.Set(0, -10.0f);
	m_world = new b2World(gravity);
	
	b2BodyDef bodyDef;
	bodyDef.position.Set(0, 0);
	b2Body* groundBody = m_world->CreateBody(&bodyDef);
	b2EdgeShape groundBox;

	groundBox.Set(b2Vec2(0, 0), b2Vec2(size.width/PTM_RADIO, 0));
	groundBody->CreateFixture(&groundBox, 0);
	groundBox.Set(b2Vec2(0, size.height / PTM_RADIO), b2Vec2(size.width / PTM_RADIO, size.height / PTM_RADIO));
	groundBody->CreateFixture(&groundBox, 0);
	groundBox.Set(b2Vec2(0, 0), b2Vec2(0, size.height / PTM_RADIO));
	groundBody->CreateFixture(&groundBox, 0);
	groundBox.Set(b2Vec2(size.width / PTM_RADIO, 0), b2Vec2(size.width / PTM_RADIO, size.height / PTM_RADIO));
	groundBody->CreateFixture(&groundBox, 0);
}

void BackGroundLayer2::addNewSpriteWithCoords(cocos2d::CCPoint location, cocos2d::CCSprite * pSprite)
{
	pSprite->setPosition(location);
	
	b2BodyDef spriteBodyDef;
	spriteBodyDef.type = b2_dynamicBody;
	spriteBodyDef.position.Set(location.x / PTM_RADIO, location.y / PTM_RADIO);
	spriteBodyDef.userData = pSprite;

	b2Body* spriteBody = m_world->CreateBody(&spriteBodyDef);
	b2CircleShape spriteShape;
	spriteShape.m_radius = 17.0f/ PTM_RADIO;
	b2FixtureDef spriteFixture;
	spriteFixture.shape = &spriteShape;
	spriteFixture.density = 1.0f;
	spriteFixture.friction = 0.3f;
	spriteFixture.userData = pSprite;
	spriteBody->CreateFixture(&spriteFixture);

	this->addChild(pSprite);
}

void BackGroundLayer2::CallCloseBack(cocos2d::CCObject * sender)
{
	/*Director::getInstance()->end();*/
	CCUserDefault::sharedUserDefault()->setStringForKey("Bill", "string");
	UserRecord userRecord("Tom");
	userRecord.setCoin(100);
	userRecord.setExp(1000);
	userRecord.setLife(2);
	userRecord.SaveToUserDefault();
}

void BackGroundLayer2::onTouchesEnded(const std::vector<cocos2d::Touch*>& touches, cocos2d::Event * unused_event)
{
	for (auto it = touches.begin(); it != touches.end(); ++it)
	{
		CCPoint location = (*it)->getLocation();
		CCSprite* sprite = CCSprite::create("icona.png");
		sprite->setScale(0.5f);
		addNewSpriteWithCoords(location, sprite);
	}
}

void BackGroundLayer2::update(float dt)
{
	int32 velocityNums = 8;
	int32 positionNums = 1;
	m_world->Step(dt, 8, 1);
	for (b2Body* it = m_world->GetBodyList(); it; it = it->GetNext())
	{
		if (it->GetUserData() != nullptr)
		{
			CCSprite* sprite = reinterpret_cast<CCSprite*>(it->GetUserData());
			sprite->setPosition(it->GetPosition().x*PTM_RADIO,it->GetPosition().y*PTM_RADIO);
			sprite->setRotation(-1 * CC_RADIANS_TO_DEGREES(it->GetAngle()));
		}
	}
}
